package com.zhuoshi.demo;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.zhuoshi.bean.Person;
import com.zhuoshi.dao.PersonDao;

public class Ann {
	SqlSessionFactory factory;
	@Before
	public void before() throws IOException{
		InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");
		factory = new SqlSessionFactoryBuilder().build(is);
		factory.getConfiguration().addMapper(PersonDao.class);
	}
	/**
	 * 查询单个对象
	 */
	@Test
	public void selectByPid(){
		SqlSession session = factory.openSession();
		PersonDao dao = session.getMapper(PersonDao.class);
		Person person = dao.selectByPid(1);
		System.out.println(person);
		session.close();
	}
	/**
	 * 查询所有对象
	 */
	@Test
	public void selectAll(){
		SqlSession session = factory.openSession();
		PersonDao dao = session.getMapper(PersonDao.class);
		List<Person> list = dao.selectAll();
		for (Person p : list) {
			System.out.println(p);
		}
		session.close();
	}
	
	/**
	 * 通过名字 模糊查询
	 */
	@Test
	public void selectByNameLike(){
		SqlSession session = factory.openSession();
		PersonDao dao = session.getMapper(PersonDao.class);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "李");
		List<Person> list = dao.selectByNameLike(map );
		for (Person p : list) {
			System.out.println(p);
		}
		session.close();
	}
	
	/**
	 * 插入
	 */
	@Test
	public void insert(){
		SqlSession session = factory.openSession();
		PersonDao dao = session.getMapper(PersonDao.class);
		try {
			Person person = new Person();
			person.setPname("完蛋");
			person.setAge(21);
			dao.insert(person);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally{
			session.close();
		}
	}
	
	/**
	 * 更新
	 */
	@Test
	public void update(){
		SqlSession session = factory.openSession();
		PersonDao dao = session.getMapper(PersonDao.class);
		try {
			Person person = new Person();
			person.setPid(10);
			person.setPname("完蛋了");
			person.setAge(22);
			dao.update(person);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally{
			session.close();
		}
	}
	
	/**
	 * 删除
	 */
	@Test
	public void delete(){
		SqlSession session = factory.openSession();
		PersonDao dao = session.getMapper(PersonDao.class);
		try {
			dao.delete(10);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally{
			session.close();
		}
	}
	
	/**
	 * 关联关系 一对多
	 */
	@Test
	public void selectPersonAndOrders(){
		SqlSession session = factory.openSession();
		PersonDao dao = session.getMapper(PersonDao.class);
		Person person = dao.selectPersonAndOrders(5);
		System.out.println(person.getOrdersList().size());
		session.close();
	}
}
